<?php
/**
 * +----------------------------------------------------------
 * ROM模块
 * +----------------------------------------------------------
 */
class RomAction extends CommonAction{
	/**
	 * +----------------------------------------------------------
	 * ROM首页
	 * +----------------------------------------------------------
	 */
	public function index(){
		$channelname = MODULE_NAME;
		//$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		$ChannelModel = M('Channel');
		$type = $ChannelModel->where("status=1 AND action = '$channelname'")->find();
		$id = $type['id'];
		$map = D('Common')->getChannelMap($id);
		$map['status'] = array('eq',1);	
		//分页取数据
		import("ORG.Util.Page");
		$Rom = D("Rom");			
		$count = $Rom->where($map)->count(); 
		$Page = new Page($count,8);
		$show = $Page->show(); 
		$list = $Rom->where($map)->order('sort DESC,dateline DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $key=>$val){
			$list[$key]=$this->changurl($val);
		}
		//获取手机品牌
		$PhoneBrand = D('PhoneType')->where('status = 1 and pid = 0')->select();
		foreach ($PhoneBrand as $key => $value){
			$PhoneBrand[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/c" , array('xh' => $value['id']));
		}
		//获取系统版本
		$SystemVer = getSystemVerList();
		foreach($SystemVer as $key => $value){
			$SystemVer[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/c" , array('xt' => $value['id']));
		}
		//赋值给模板
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('PhoneBrand' , $PhoneBrand);
		$this->assign('SystemVer' , $SystemVer);
		$this->seo($type['title'], $type['keywords'], $type['description'], D('Common')->getPosition($id,1));
		$this->choosetpl($type);
	}
	
	/**
	 * +----------------------------------------------------------
	 * ROM条件筛选
	 * +----------------------------------------------------------
	 */
	public function s(){
		$id = $this->newrouter(MODULE_NAME,ACTION_NAME);
		$type = D('Category')->where('status=1')->find($id);
		$map = D('Common')->getRomCategoryMap($id);
		$map['status'] = array('eq',1);	
		
		//获取手机品牌
		$PhoneBrand = D('PhoneType')->where('status = 1 and pid = 0')->select();
		foreach ($PhoneBrand as $key => $value){
			$PhoneBrand[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/s" , array('id' => $id,'xh' => $value['id']));
		}
		//获取系统版本
		$SystemVer = getSystemVerList();
		foreach($SystemVer as $key => $value){
			$SystemVer[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/s" , array('id' => $id,'xt' => $value['id']));
		}

		//分页取数据
		import("ORG.Util.Page");
		$Rom = D("Rom");			
		$count = $Rom->where($map)->count(); 
		$Page = new Page($count,8);
		$show = $Page->show(); 
		$list = $Rom->where($map)->order('sort DESC,dateline DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $key=>$val){
			$list[$key]=$this->changurl($val);
		}
		
		//赋值给模板
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('PhoneBrand' , $PhoneBrand);
		$this->assign('SystemVer' , $SystemVer);
		$this->seo($type['title'], $type['keywords'], $type['description'], D('Common')->getPosition($id));
		$this->choosetpl($type);
	}
	
	/**
	 * +----------------------------------------------------------
	 * ROM列表页
	 * +----------------------------------------------------------
	 */
	public function c(){
		$id = $this->newrouter(MODULE_NAME,ACTION_NAME);
		//$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		$phonetype = empty ($_GET['xh']) ? 0 : intval($_GET['xh']);
		$systemver = empty ($_GET['xt']) ? 0 : intval($_GET['xt']);
		
		if($id){
			$type = D('Category')->where('status=1')->find($id);
			$map = D('Common')->getRomCategoryMap($id);
		}else{
			$channelname = MODULE_NAME;
			$ChannelModel = M('Channel');
			$type = $ChannelModel->where("status=1 AND action = '$channelname'")->find();
			$map = D('Common')->getChannelMap($type['id']);
		}

		//获取手机品牌
		$PhoneBrand = D('PhoneType')->where('status = 1 and pid = 0')->select();
		
		//获取手机型号
		$PhoneType = getPhoneTypeList($phonetype);
		//获取系统版本
		$SystemVer = getSystemVerList();
		
		if($PhoneType){
			$map['phone_type'] = $PhoneType['map'];
		}
		if($systemver){
			$map['system_ver'] = array('eq' , $systemver);
		}
		//手机型号与系统版本url重组
		$PhoneTypePara = $SystemVerPara = $PhoneBrandPara = array();
		$url = $_SERVER['PATH_INFO'];
		$exp = explode('/', $url);
		if($id && array_search('id',$exp)){
			$PhoneTypePara['id'] = $SystemVerPara['id'] = $PhoneBrandPara['id'] = $id;
		}
		if($systemver){
			$PhoneTypePara['xt'] = $systemver;
		}
		if($phonetype){
			$SystemVerPara['xh'] = $phonetype;
		}
		//手机型号url
		foreach ($PhoneType as $key => $value){
			$PhoneTypePara['xh'] = $value['id'];
			$PhoneType[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/".ACTION_NAME , $PhoneTypePara);
		}
		//系统版本url
		foreach($SystemVer as $key => $value){
			$SystemVerPara['xt'] = $value['id'];
			$SystemVer[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/".ACTION_NAME , $SystemVerPara);
		}
		//手机品牌url
		foreach ($PhoneBrand as $key => $value){
			$PhoneBrandPara['xh'] = $value['id'];
			$PhoneBrand[$key]['url'] = U(APP_NAME."://".MODULE_NAME."/".ACTION_NAME , $PhoneBrandPara);
		}
		$map['status'] = array('eq',1);	
		//分页取数据
		import("ORG.Util.Page");
		$Rom = D("Rom");			
		$count = $Rom->where($map)->count(); 
		$Page = new Page($count,8);
		$show = $Page->show(); 
		$list = $Rom->where($map)->order('sort DESC,dateline DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $key=>$val){
			$list[$key]=$this->changurl($val);
		}
		
		//赋值给模板
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('PhoneType' , $PhoneType);
		$this->assign('PhoneBrand' , $PhoneBrand);
		$this->assign('SystemVer' , $SystemVer);
		$this->seo($type['title'], $type['keywords'], $type['description'], D('Common')->getPosition($id), D('Common')->getClassPosition($id));
		//$this->choosetpl($type);
		$this->display("Rom:list");
	}
	
	/**
	 * +----------------------------------------------------------
	 * ROM内容
	 * +----------------------------------------------------------
	 */
	public function view(){	
		$id = $this->router();
		//$id = $_GET['id'];
		//访问次数统计
		$Ip = get_client_ip();
		$ModuleViewCount = D('ViewCount');
		$ViewCount = $ModuleViewCount->where("`module_id` = $id and `ip` = '$Ip' and `idtype` = 'Rom'")->find();
		if(!$ViewCount){
			$data = array(
				'module_id' => $id,
				'ip' => $Ip,
				'idtype' => 'Rom',
				'dateline' => time(),
			);
			$ModuleViewCount->data($data)->add();
			D('Rom')->setInc('romapv',"id=$id",1);
		}
		
		$info = D('Rom')->where("id=$id AND status=1")->find();

		//格式化下载地址
		$download_link = explode('|', $info['download_link']);
		if($download_link[0]){
			$info['download_link'] = '';
			foreach ($download_link as $key => $value){
				$info['download_link'] .= "<a href='$value' target='_blank'>下载地址".($key + 1)."</a> ";
			}
		}
		
		$this->assign('info',$info);
		$this->seo($info['title'], $info['keywords'], $info['description'], D('Common')->getPosition($info['cid']), D('Common')->getClassPosition($info['cid']));
		
		$art_pre = D('Rom')->where("id<$id AND status=1")->order('id DESC')->field('id,title,romapv')->find();
		$art_pre = $this->changurl($art_pre);
		$this->assign('art_pre',$art_pre);//上一篇
		
		$art_next = D('Rom')->where("id>$id AND status=1")->order('id')->field('id,title,romapv')->find();
		$art_next = $this->changurl($art_next);
		$this->assign('art_next',$art_next);//下一篇
		
		$art_rand = D('Rom')->where("status=1")->order('rand()')->limit(8)->select();
		foreach ($art_rand as $key => $val){
			$art_rand[$key] = $this->changurl($val);
		}
		$this->assign('art_rand',$art_rand);//随机8篇
		
		$about_art = D('Article')->where("status = 1 AND phone_type = ".$info['phone_type'])->order('add_time DESC')->limit(10)->select();
		foreach ($about_art as $key => $val){
			$about_art[$key] = $this->changurl($val);
		}
		$this->assign('about_art' , $about_art);//相关文章
		
		$message = D('Message')->where("aid=$id AND status=1 AND pid=0")->select();
		if(is_array($message)){
			foreach ($message as $key=>$val){
				$message[$key] = $this->msgmodify($val);
				$message[$key]['reply'] = D('Message')->where('status=1 AND pid='.$val['id'])->select();
				foreach ($message[$key]['reply'] as $key2 => $val2){
					$message[$key]['reply'][$key2] = $this->msgmodify($val2);
				}				
			}
		}
		$this->assign('msg_list',$message);//评论
		
		$about_rom = D('Rom')->where('status = 1 AND phone_type = '.$info['phone_type'])->limit(10)->order('romapv desc,dateline desc')->select();
		$this->assign('about_rom',$about_rom);
		
		$this->choosetpl($info);
		
	}
	
	//空操作
	public function _empty(){
		$module = MODULE_NAME;
		$action = ACTION_NAME;
		$channel = getCheannelId($module);
		$r = D("Router")->where("rewrite='".$action."' AND channel = '$channel'")->getField('url');
		if($r){
			$exp = explode('/', $r);
			R(ucfirst($module), $exp['0']);
		}else{
			$this->redirect("Index/index");
		}	
	}
}
?>
